
#include "StringUtils.h"

bool StringToBool(const BString& text)
{
	return text == "true";
}
BString StringFromBool(bool b)
{
	return b ? "true" : "false";
}

BString StringFromSpace(const BSpace& space)
{
	BString text;
	text += BString(space.min().x()) + ',';
	text += BString(space.max().x()) + ',';
	text += BString(space.min().y()) + ',';
	text += BString(space.max().y()) + ',';
	text += BString(space.min().z()) + ',';
	text += BString(space.max().z());
	return text;
}
BSpace StringToSpace(const BString& text)
{
	BSpace space;
	BStringList words = text.split(',');
	if (words.size() == 6)
	{
		space.min().x() = words[0].toFloat();
		space.max().x() = words[1].toFloat();
		space.min().y() = words[2].toFloat();
		space.max().y() = words[3].toFloat();
		space.min().z() = words[4].toFloat();
		space.max().z() = words[5].toFloat();
	}
	return space;
}

BString StringFromVector(const BVector& vec)
{
	BString text;
	text += BString(vec.x(), 3) + ',';
	text += BString(vec.y(), 3) + ',';
	text += BString(vec.z(), 3);
	return text;
}
BVector StringToVector(const BString& text)
{
	BStringList words = text.split(',');
	if (words.size() == 3)
	{
		float x = words[0].toFloat();
		float y = words[1].toFloat();
		float z = words[2].toFloat();
		return BVector(x, y, z);
	}
	return BVector();
}

BColor StringToColor(const BString& text)
{
	BStringList words = text.split(',');
	if (words.size() == 4)
	{
		BByte r = words[0].toInt();
		BByte g = words[1].toInt();
		BByte b = words[2].toInt();
		BByte a = words[2].toInt();
		return BColor(r, g, b, a);
	}
	if (words.size() == 3)
	{
		BByte r = words[0].toInt();
		BByte g = words[1].toInt();
		BByte b = words[2].toInt();
		return BColor(r, g, b, 255);
	}
	return BColor();

}
BString StringFromColor(const BColor& color)
{
	BString text;
	text += BString(color.r()) + ',';
	text += BString(color.g()) + ',';
	text += BString(color.b()) + ',';
	text += BString(color.a());
	return text;
}

BString ConvertRangeToString(const BRangef& range)
{
	BString text;
	text += BString(range.min()) + ',';
	text += BString(range.max());
	return text;
}
BRangef ConvertStringToRange(const BString& text)
{
	BStringList words = text.split(',');
	if (words.size() == 2)
	{
		float minimum = words[0].toFloat();
		float maximum = words[1].toFloat();
		return BRangef(minimum, maximum);
	}
	return BRangef();
}

BSize StringToSize(const BString& text)
{
	BStringList words = text.split(',');
	if (words.size() == 2)
	{
		int w = words[0].toInt();
		int h = words[1].toInt();
		return BSize(w, h);
	}
	return BSize();
}
BString StringFromSize(const BSize& size)
{
	BString text;
	text += BString(size.width()) + ',';
	text += BString(size.height());
	return text;
}

BSizef StringToSizef(const BString& text)
{
	BStringList words = text.split(',');
	if (words.size() == 2)
	{
		float w = words[0].toFloat();
		float h = words[1].toFloat();
		return BSizef(w, h);
	}
	return BSizef();
}
BString StringFromSizef(const BSizef& size)
{
	BString text;
	text += BString(size.width()) + ',';
	text += BString(size.height());
	return text;
}

BRect StringToRect(const BString& text)
{
	BStringList words = text.split(',');
	if (words.size() == 4)
	{
		int x = words[0].toInt();
		int y = words[1].toInt();
		int w = words[2].toInt();
		int h = words[3].toInt();
		return BRect(x, y, w, h);
	}
	return BRect();
}
BString StringFromRect(const BRect& rect)
{
	BString text;
	text += BString(rect.x()) + ',';
	text += BString(rect.y()) + ',';
	text += BString(rect.width()) + ',';
	text += BString(rect.height());
	return text;
}

BMargin StringToMargin(const BString& text)
{
	BStringList words = text.split(',');
	if (words.size() == 4)
	{
		int left = words[0].toInt();
		int top = words[1].toInt();
		int right = words[2].toInt();
		int bottom = words[3].toInt();
		return BMargin(left, top, right, bottom);
	}
	return BMargin();
}
BString StringFromMargin(const BMargin& margin)
{
	BString text;
	text += BString(margin.left()) + ',';
	text += BString(margin.top()) + ',';
	text += BString(margin.right()) + ',';
	text += BString(margin.bottom());
	return text;
}
